package com.hs.dnms.action.search;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hs.core.action.BaseAction;
import com.hs.core.db.DBMgr;
import com.hs.core.db.QueryPage;
import com.hs.core.utils.SystemUtil;
import com.hs.dnms.model.DnmsMagazine;
import com.hs.dnms.service.IDataService;
@Controller  
public class SearchController  extends BaseAction {
    /**
	 * 
	 */
	private static final long serialVersionUID = -354434172898440670L;
	private static final Logger logger = Logger.getLogger(SearchController.class);
	private IDataService dataService;
    @Autowired  
	public void setDataService(IDataService dataService) {
		this.dataService = dataService;
	}
    /**
	  * 搜索主页
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/search/sindex.do")   
    public ModelAndView sindex(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 DBMgr dbo = DBMgr.getDbUtilInst(request.getSession().getServletContext());
		 List typelist = dbo.queryList("select DICTCODE as DOCUMENTTYPE,(select count(*)  from DNMS_MAGATYPE where DICTCODE=a.dictcode) as countall from hs_code a where DICTTYPE=20 and dictcode !='0' order by ORDNUM", null);
		 List catalist = dbo.queryList("select DICTCODE as DOCCATAGORY,(select count(*)  from DNMS_MAGACATAGORY where DICTCODE=a.dictcode) as countall from hs_code a where DICTTYPE=21 and dictcode !='0' order by ORDNUM", null);
		 modelMap.put("typelist", typelist);
		 modelMap.put("catalist", catalist);
	     modelMap.put("searchtype", "1");
	     
		 return new ModelAndView("app/search/resultlist",modelMap);
	 } 
	 /**
	  * 搜索主页
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/search/sindex3.do")   
     public ModelAndView sindex3(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 DBMgr dbo = DBMgr.getDbUtilInst(request.getSession().getServletContext());
		 List typelist = dbo.queryList("select DICTCODE as DOCUMENTTYPE,(select count(*)  from DNMS_MAGATYPE where DICTCODE=a.dictcode) as countall from hs_code a where DICTTYPE=20 and dictcode !='0' order by ORDNUM", null);
		 List catalist = dbo.queryList("select DICTCODE as DOCCATAGORY,(select count(*)  from DNMS_MAGACATAGORY where DICTCODE=a.dictcode) as countall from hs_code a where DICTTYPE=21 and dictcode !='0' order by ORDNUM", null);
		 modelMap.put("typelist", typelist);
		 modelMap.put("catalist", catalist);
		 //检索类型,1是检索，2是二次检索 
		 String secondType = request.getParameter("secondType");
		 if(StringUtils.isNotBlank(secondType)){
			 modelMap.put("secondType", secondType);
		 }
		 //排序
		 String orderName = request.getParameter("orderName");
		 String orderNum = request.getParameter("orderNum");
		 StringBuffer orderByStr = new StringBuffer(" order by ");
		 if(StringUtils.isNotBlank(orderName)){
		    	if("1".equals(orderNum)){
		    		orderByStr.append(orderName).append(" asc");
		    	}
		    	if("2".equals(orderNum)){
		    		orderByStr.append(orderName).append(" desc");
		    	}
		    }else{
		    	orderByStr.append("a.maganame").append(" desc");
		    	orderName = "a.maganame";
		    	orderNum = "2";
		    }
		 request.setAttribute("orderName", orderName); 
		 request.setAttribute("orderNum", orderNum); 
		 
		 StringBuffer allfullsql = new StringBuffer(" select a.*,b.DISEASENAME,b.DISEASENAMEN,b.indexid from DNMS_MAGAZINE a left join DNMS_DISEASE b on a.id = b.indexid  where 1=1 ");
		 String dtype = request.getParameter("DOCUMENTTYPE");
		 String dgory = request.getParameter("DOCCATAGORY");
		 if(null != dtype && !"".equals(dtype)){
			 allfullsql.append("and a.ID IN(SELECT MAGAID FROM DNMS_MAGATYPE WHERE DICTCODE='");
			 allfullsql.append(dtype);
			 allfullsql.append("')");
		 }
		 if(null != dgory && !"".equals(dgory)){
			 allfullsql.append("and a.ID IN(SELECT MAGAID FROM DNMS_MAGACATAGORY WHERE DICTCODE='");
			 allfullsql.append(dgory);
			 allfullsql.append("')");
		 }
		 //年份
		 String startyear_g = request.getParameter("startyear_g");
		 String endyear_g = request.getParameter("endyear_g");
		 if(null != startyear_g && !"".equals(startyear_g)){
			 allfullsql.append(" and to_number(substr(a.year,0,4))>=");
			 allfullsql.append(startyear_g);
			 allfullsql.append(" ");
		 }
		 if(null != endyear_g && !"".equals(endyear_g)){
			 allfullsql.append(" and to_number(substr(a.year,0,4))<=");
			 allfullsql.append(endyear_g);
			 allfullsql.append(" ");
		 }
		 //语种
		 String g_language = request.getParameter("g_language");
		 if(null != g_language && !"".equals(g_language)){
			 if("1".equals(g_language)){
				 allfullsql.append(" and a.language ='中文' ");
			 }else if("2".equals(g_language)){
				 allfullsql.append(" and a.language ='英文' ");
			 }
		 }
		 //来源期刊
		 String g_sourcemag = request.getParameter("g_sourcemag");
		 if(null != g_sourcemag && !"".equals(g_sourcemag)){
			 if("1".equals(g_sourcemag)){
				 allfullsql.append(" and a.sourcemagazine ='SCI期刊' ");
			 }else if("2".equals(g_sourcemag)){
				 allfullsql.append(" and a.sourcemagazine ='中文核心期刊' ");
			 }
		 }
		 allfullsql.append(createSql(request,"txt_1","maganame")==null?"":createSql(request,"txt_1","maganame"));
		 allfullsql.append(createSql(request,"txt_2","themewords")==null?"":createSql(request,"txt_2","themewords"));
		 allfullsql.append(createSql(request,"txt_3","keywords")==null?"":createSql(request,"txt_3","keywords"));
		 allfullsql.append(createSql(request,"txt_4","abstract")==null?"":createSql(request,"txt_4","abstract"));
		 allfullsql.append(createSql(request,"txt_5","author")==null?"":createSql(request,"txt_5","author"));
		 allfullsql.append(createSql(request,"txt_6","authorunit")==null?"":createSql(request,"txt_6","authorunit"));
		 allfullsql.append(createSql(request,"txt_7","periodicalname")==null?"":createSql(request,"txt_7","periodicalname"));
		 allfullsql.append(createSql(request,"txt_8","fundtype")==null?"":createSql(request,"txt_8","fundtype"));

		 if("2".equals(secondType)){
			 //如果是二次检索加上上一次的条件
			 String onesql = request.getParameter("onesql");
			 if(StringUtils.isNotBlank(onesql)){
				 onesql = "select id from(" + onesql + ")";
				 allfullsql.append(" and a.id in (");
				 allfullsql.append(onesql);
				 allfullsql.append(")");
			 }
		 }
		 allfullsql.append(orderByStr);
		 
		 System.out.println("allfullsql::" + allfullsql);
		 Map conditionmap = new HashMap();
         conditionmap.put("orderName", orderName);
         conditionmap.put("orderNum", orderNum);
		 String perPageCount = request.getParameter("perPageCount");
	     QueryPage qp = new QueryPage(request,request.getContextPath()+"/search/sindex3.do",allfullsql.toString(),conditionmap); 
//	     if(null == perPageCount || "".equals(perPageCount)){
//			qp.setPerPageCount(8);
//		 }else {
//			qp.setPerPageCount(Integer.parseInt(perPageCount));
//		 }
	     qp.query();  
	     String pagenum =  qp.getCurrentPageNo() + "";
	     String pagecount = qp.getPerPageCount() + "";
	     modelMap.put("pagenum", pagenum);
	     modelMap.put("pagecount", pagecount);
	     modelMap.put("pagestr", qp.getPageStr());
	     modelMap.put("datalist", qp.getDataList());
	     modelMap.put("allsql", allfullsql.toString());
	     modelMap.put("searchtype", "1");
	     String statinfo = statinfo(request,allfullsql.toString());
	     modelMap.put("statinfo",statinfo);
	     String searchtype = request.getParameter("searchtype");
	     if(StringUtils.isEmpty(searchtype))
	    	 searchtype = "1";
	     modelMap.put("searchtype", searchtype);
	     if(!"0".equals(secondType)){
	    	 modelMap.put("onesql", allfullsql.toString());
	     }
		 return new ModelAndView("app/search/booklist",modelMap);
		 
	 } 
	 private String createSql(HttpServletRequest request,String cname,String dbname){
		 StringBuffer sqlbuffer = new StringBuffer();
		 //值
		 String c_value = cname+"_value";
		 //逻辑关系，并且 或者
		 String c_logical = cname+"_logical";
		 //字段的名字
		 String c_sel = cname+"_sel";
		 //精确或模糊
		 String c_special = cname+"_special";
		 
		 String cvalue = request.getParameter(c_value);
		 String clogical = request.getParameter(c_logical);
		 String csel = request.getParameter(c_sel);
		 String cspecial = request.getParameter(c_special);
		 request.setAttribute(c_value, cvalue);
		 request.setAttribute(c_logical, c_logical);
		 request.setAttribute(c_sel, c_sel);
		 request.setAttribute(c_special, c_special);
		 System.out.println("value:::" + clogical + "," + csel + "," + cvalue + "," + cspecial);
//		 if(StringUtils.isEmpty(cspecial))
//			 cspecial = "1";//默认为模糊检索
		 if(null == cvalue || StringUtils.isEmpty(cvalue)){//查询条件为空时返回空
			 return null;
		 }
		 if("0".equals(clogical)){
			 //0 and 关系  1：或者关系
			 sqlbuffer.append(" and "); 
		 }else if("1".equals(clogical)){
			 //0 and 关系  1：或者关系
			 sqlbuffer.append(" or "); 
		 }else{
			 sqlbuffer.append(" and "); 
		 }
		 sqlbuffer.append(csel);
		 if("0".equals(cspecial)){
			 //0 精确  1：模糊
			 sqlbuffer.append(" ='"); 
			 sqlbuffer.append(cvalue);
			 sqlbuffer.append("'");
		 }
		 if("1".equals(cspecial)){
			//0 精确  1：模糊
			 sqlbuffer.append(" like '%"); 
			 sqlbuffer.append(cvalue);
			 sqlbuffer.append("%'");
		 }
		 return sqlbuffer.toString();
	 }
	 
	 /**
	  * 查询提交action
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/search/doSearch.do")   
     public ModelAndView doSearch(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 DBMgr dbo = DBMgr.getDbUtilInst(request.getSession().getServletContext());
		 String orderName = request.getParameter("orderName");
		 String orderNum = request.getParameter("orderNum");
		 StringBuffer orderByStr = new StringBuffer(" order by ");
		 if(StringUtils.isNotBlank(orderName)){
		    	if("1".equals(orderNum)){
		    		orderByStr.append(orderName).append(" asc");
		    	}
		    	if("2".equals(orderNum)){
		    		orderByStr.append(orderName).append(" desc");
		    	}
		    }else{
		    	orderByStr.append("a.maganame").append(" desc");
		    	orderName = "a.maganame";
		    	orderNum = "2";
		    }
		 request.setAttribute("orderName", orderName); 
		 request.setAttribute("orderNum", orderNum); 
		 StringBuffer allfullsql = new StringBuffer(" select a.*,b.DISEASENAME,b.DISEASENAMEN,b.indexid from DNMS_MAGAZINE a left join DNMS_DISEASE b on a.id = b.indexid  where 1=1 ");
		 //文献分类和类型
		 String dtype = request.getParameter("DOCUMENTTYPE");
		 String dgory = request.getParameter("DOCCATAGORY");
		 if(null != dtype && !"".equals(dtype)){
			 allfullsql.append("and a.ID IN(SELECT MAGAID FROM DNMS_MAGATYPE WHERE DICTCODE='");
			 allfullsql.append(dtype);
			 allfullsql.append("')");
		 }
		 if(null != dgory && !"".equals(dgory)){
			 allfullsql.append("and a.ID IN(SELECT MAGAID FROM DNMS_MAGACATAGORY WHERE DICTCODE='");
			 allfullsql.append(dgory);
			 allfullsql.append("')");
		 }
		 //年度
		 String startyear_s = request.getParameter("startyear_s");
		 String endyear_s = request.getParameter("endyear_s");
		 if(null != startyear_s && !"".equals(startyear_s)){
			 allfullsql.append(" and substr(a.year,0,4)>='");
			 allfullsql.append(startyear_s);
			 allfullsql.append("' ");
		 }
		 if(null != endyear_s && !"".equals(endyear_s)){
			 allfullsql.append(" and substr(a.year,0,4)<='");
			 allfullsql.append(endyear_s);
			 allfullsql.append("' ");
		 }
		 //治疗方法
		 allfullsql.append(specialSql(request,"pro_1")==null?"":specialSql(request,"pro_1"));
         allfullsql.append(specialSql(request,"pro_2")==null?"":specialSql(request,"pro_2"));
         allfullsql.append(specialSql(request,"pro_3")==null?"":specialSql(request,"pro_3"));
         allfullsql.append(specialSql(request,"pro_4")==null?"":specialSql(request,"pro_4"));
         allfullsql.append(specialSql(request,"pro_5")==null?"":specialSql(request,"pro_5"));
         allfullsql.append(specialSql(request,"pro_6")==null?"":specialSql(request,"pro_6"));
         allfullsql.append(specialSql(request,"pro_7")==null?"":specialSql(request,"pro_7"));
         allfullsql.append(specialSql(request,"pro_8")==null?"":specialSql(request,"pro_8"));
         
		 allfullsql.append(createSql(request,"spec_1","maganame")==null?"":createSql(request,"spec_1","maganame"));
		 allfullsql.append(createSql(request,"spec_2","themewords")==null?"":createSql(request,"spec_2","themewords"));
		 allfullsql.append(createSql(request,"spec_3","keywords")==null?"":createSql(request,"spec_3","keywords"));
		 allfullsql.append(createSql(request,"spec_4","abstract")==null?"":createSql(request,"spec_4","abstract"));
		 allfullsql.append(createSql(request,"spec_5","author")==null?"":createSql(request,"spec_5","author"));
		 allfullsql.append(createSql(request,"spec_6","authorunit")==null?"":createSql(request,"spec_6","authorunit"));
		 allfullsql.append(createSql(request,"spec_7","periodicalname")==null?"":createSql(request,"spec_7","periodicalname"));
		 allfullsql.append(createSql(request,"spec_8","fundtype")==null?"":createSql(request,"spec_8","fundtype"));
		 
		 allfullsql.append(orderByStr);
		 
		 System.out.println("allfullsql::" + allfullsql);
		 
		 Map conditionmap = new HashMap();
         conditionmap.put("orderName", orderName);
          conditionmap.put("orderNum", orderNum);
	     QueryPage qp = new QueryPage(request,request.getContextPath()+"/search/doSearch.do",allfullsql.toString(),conditionmap); 
	     //qp.setPerPageCount(8);
	     qp.query();  
	     String pagenum =  qp.getCurrentPageNo() + "";
	     String pagecount = qp.getPerPageCount() + "";
	     modelMap.put("pagenum", pagenum);
	     modelMap.put("pagecount", pagecount);
	     modelMap.put("pagestr", qp.getPageStr());
	     modelMap.put("datalist", qp.getDataList());
	     modelMap.put("allsql", allfullsql.toString());
	     String searchtype = request.getParameter("searchtype");
	     if(StringUtils.isEmpty(searchtype))
	    	 searchtype = "2";
	     modelMap.put("searchtype", searchtype);
	     String statinfo = statinfo(request,allfullsql.toString());
	     logger.debug("statinfo:" +statinfo);
	     modelMap.put("statinfo", statinfo);
		 return new ModelAndView("app/search/booklist",modelMap);
		 
	 } 
	 public String specialSql(HttpServletRequest request,String cname){
		 StringBuffer sbsql = new StringBuffer();
		//值
		 String c_value = cname+"_value";
		 //逻辑关系，并且 或者
		 String c_logical = cname+"_logical";
		 //字段的名字
		 String c_sel = cname+"_sel";
		 //精确或模糊
		 String c_special = cname+"_special";
		//正名或异名查询
		 String c_syn = cname+"_syn";
		 
		 String cvalue = request.getParameter(c_value);
		 String clogical = request.getParameter(c_logical);
		 String csel = request.getParameter(c_sel);
		 String cspecial = request.getParameter(c_special);
		 String csyn = request.getParameter(c_syn);
         if(null != csel && !"".equals(csel)){
        	 if("diseasename".equals(csel)){
        		 //中医疾病
        		 String jibingsql =  this.sql4jibing(cname,request);
        		 if(null != jibingsql){
        			 if("1".equals(clogical))
        				 sbsql.append(" or b.id in").append( jibingsql);
        			 else 
        				 sbsql.append(" and b.id in").append( jibingsql);
        		 }
        	 }else if("diseasenamen".equals(csel)){
        		 //西医疾病
        		 String xiyisql = this.sql4xiyi(cname,request);
        		 if(null != xiyisql){
        			 if("1".equals(clogical))
        				 sbsql.append(" or b.id in").append( xiyisql);
        			 else 
        				 sbsql.append(" and b.id in").append( xiyisql);
        		 }
        		 System.out.println("sbsql::" + sbsql.toString());
        	 }else if("paradisname".equals(csel)){
        		 //并发病
        		 String zhongyisql = this.sql4bingfabing(cname,request);
        		 if(null != zhongyisql){
        			 if("1".equals(clogical))
        				 sbsql.append(" or b.id in").append( zhongyisql);
        			 else
        				 sbsql.append(" and b.id in").append( zhongyisql);
        		 }
        	 }else if("symptomname".equals(csel)){
        		 //证候
        		 String zhenghousql = this.sql4zhenghou(cname,request);
        		 if(null != zhenghousql){
        			 if("1".equals(clogical))
        				 sbsql.append(" or b.id in").append( zhenghousql);
        			 else
        				 sbsql.append(" and b.id in").append( zhenghousql);
        		 }
        	 }else if("prename".equals(csel)){
        		//方剂
        		 String fangjisql =  this.sql4fangji(cname,request);
        		 if(null != fangjisql){
        			 if("1".equals(clogical))
        				 sbsql.append(" or b.id in").append( fangjisql);
        			 else
        				 sbsql.append(" and b.id in").append( fangjisql);
        		 }
        			 
        	 }else if("herbname".equals(csel)){
        		//中药
        		 String zhongyaosql = this.sql4zhongyao(cname,request);
        		 if(null != zhongyaosql){
        			 if("1".equals(clogical))
        				 sbsql.append(" or b.id in").append( zhongyaosql);
        			 else
        				 sbsql.append(" and b.id in").append( zhongyaosql);
        		 }
        	 }else if("pgroupname".equals(csel)){
        		 //针灸组
        		 String xueweizusql = this.sql4xueweizu(cname,request);
        		 if(null != xueweizusql){
        			 if("1".equals(clogical))
        				 sbsql.append(" or b.id in").append( xueweizusql);
        			 else
        				 sbsql.append(" and b.id in").append( xueweizusql);
        		 }
        	 }else if("pointname".equals(csel)){
        		//穴位
        		 String xuewei_r = request.getParameter("xuewei_r");
        		 String xueweisql = this.sql4xuewei(cname,request);
        		 if(null != xueweisql){
        			 if("1".equals(clogical))
        				 sbsql.append(" or b.id in").append( xueweisql);
        			 else
        				 sbsql.append(" and b.id in").append( xueweisql);
        		 }
        	 }
         }
         return sbsql.toString();
	 }
	 /**
	  * 疾病查询
	  * @param request
	  * @return
	  */
	 private String sql4jibing(String cname ,HttpServletRequest request){
		 StringBuffer buffer = new StringBuffer(" (select id from DNMS_DISEASE d where 1=1 ");
		//值
		 String c_value = cname+"_value";
		 //逻辑关系，并且 或者
		 String c_logical = cname+"_logical";
		 //字段的名字
		 String c_sel = cname+"_sel";
		 //精确或模糊
		 String c_special = cname+"_special";
		//正名或异名查询
		 String c_syn = cname+"_syn";
		 
		 String cvalue = request.getParameter(c_value);
		 String clogical = request.getParameter(c_logical);
		 String csel = request.getParameter(c_sel);
		 String cspecial = request.getParameter(c_special);
		 String csyn = request.getParameter(c_syn);
		 
		 String istangyao = request.getParameter("istangyao");
		 String iszhongy = request.getParameter("iszhongy");
		 String iswest = request.getParameter("iswest");
		 String iszhenjiu = request.getParameter("iszhenjiu");
		 String istuina = request.getParameter("istuina");
		 String isshoushu = request.getParameter("isshoushu");
		 if("1".equals(istangyao)){
			 buffer.append(" and d.ISTANGYAO=1 ");
		 }
		 if("1".equals(iszhongy)){
			 buffer.append(" and d.ISZHONGY=1 ");
		 }
		 if("1".equals(iswest)){
			 buffer.append(" and d.iswest=1 ");
		 }
		 if("1".equals(iszhenjiu)){
			 buffer.append(" and d.ISZHENJIU=1 ");
		 }
		 if("1".equals(istuina)){
			 buffer.append(" and d.ISTUINA=1 ");
		 }
		 if("1".equals(isshoushu)){
			 buffer.append(" and d.ISSHOUSHU=1 ");
		 }
		 if(StringUtils.isEmpty(cvalue) ){
			 return null;
		 }
		 if(StringUtils.isNotEmpty(cvalue)){
			 buffer.append(" and ");
			 
			 if("0".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" DISEASENAME ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" DISEASENAME like '%");
					 buffer.append(cvalue);
					 buffer.append("%' ");
				 }
				 buffer.append(")");
			 }else if("1".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" diseasename in(select synonyms from dnms_standard where category='疾病' and standardname ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" diseasename in(select synonyms from dnms_standard where category='疾病' and standardname like '%");
					 buffer.append(cvalue);
					 buffer.append("%' ");
				 }
				 
				 buffer.append("))");
			 }else if("2".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" diseasename in(select standardname from dnms_standard where category='疾病' and synonyms ='");
					 buffer.append(cvalue);
					 buffer.append("' union select synonyms from dnms_standard where category='疾病' and standardname ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" diseasename in(select standardname from dnms_standard where category='疾病' and synonyms like '%");
					 buffer.append(cvalue);
					 buffer.append("' union select synonyms from dnms_standard where category='疾病' and standardname like '%");
					 buffer.append(cvalue); 
					 buffer.append("%' ");
				 }
				 
				 buffer.append("))");
			 }
			 
		 }
		 
		 
       return buffer.toString();
	 }
	 private String sql4xiyi(String cname ,HttpServletRequest request){
		 StringBuffer buffer = new StringBuffer(" (select id from DNMS_DISEASE d where 1=1 ");
			//值
			 String c_value = cname+"_value";
			 //逻辑关系，并且 或者
			 String c_logical = cname+"_logical";
			 //字段的名字
			 String c_sel = cname+"_sel";
			 //精确或模糊
			 String c_special = cname+"_special";
			//正名或异名查询
			 String c_syn = cname+"_syn";
			 
			 String cvalue = request.getParameter(c_value);
			 String clogical = request.getParameter(c_logical);
			 String csel = request.getParameter(c_sel);
			 String cspecial = request.getParameter(c_special);
			 String csyn = request.getParameter(c_syn);

			 String istangyao = request.getParameter("istangyao");
			 String iszhongy = request.getParameter("iszhongy");
			 String iswest = request.getParameter("iswest");
			 String iszhenjiu = request.getParameter("iszhenjiu");
			 String istuina = request.getParameter("istuina");
			 String isshoushu = request.getParameter("isshoushu");
			 if("1".equals(istangyao)){
				 buffer.append(" and d.ISTANGYAO=1 ");
			 }
			 if("1".equals(iszhongy)){
				 buffer.append(" and d.ISZHONGY=1 ");
			 }
			 if("1".equals(iswest)){
				 buffer.append(" and d.iswest=1 ");
			 }
			 if("1".equals(iszhenjiu)){
				 buffer.append(" and d.ISZHENJIU=1 ");
			 }
			 if("1".equals(istuina)){
				 buffer.append(" and d.ISTUINA=1 ");
			 }
			 if("1".equals(isshoushu)){
				 buffer.append(" and d.ISSHOUSHU=1 ");
			 }
			 if(StringUtils.isEmpty(cvalue) ){
				 return null;
			 }
			 if(StringUtils.isEmpty(cvalue) ){
				 return null;
			 }
			 if(StringUtils.isNotEmpty(cvalue)){
				 buffer.append("and ");
				 if("0".equals(csyn)){
					 if("0".equals(cspecial)){
						 buffer.append(" DISEASENAMEN ='");
						 buffer.append(cvalue);
						 buffer.append("' ");
					 }
					 if("1".equals(cspecial)){
						 buffer.append(" DISEASENAMEN like '%");
						 buffer.append(cvalue);
						 buffer.append("%' ");
					 }
					 buffer.append(")");
				 }else if("1".equals(csyn)){
					 if("0".equals(cspecial)){
						 buffer.append(" diseasenamen in(select synonyms from dnms_standard where category='疾病' and standardname ='");
						 buffer.append(cvalue);
						 buffer.append("' ");
					 }
					 if("1".equals(cspecial)){
						 buffer.append(" diseasenamen in(select synonyms from dnms_standard where category='疾病' and standardname like '%");
						 buffer.append(cvalue);
						 buffer.append("%' ");
					 }
					 buffer.append("))");
				 }else if("2".equals(csyn)){
					 if("0".equals(cspecial)){
						 buffer.append(" diseasenamen in(select standardname from dnms_standard where category='疾病' and synonyms ='");
						 buffer.append(cvalue);
						 buffer.append("' union select synonyms from dnms_standard where category='疾病' and standardname ='");
						 buffer.append(cvalue);
						 buffer.append("' ");
					 }
					 if("1".equals(cspecial)){
						 buffer.append(" diseasenamen in(select standardname from dnms_standard where category='疾病' and synonyms like '%");
						 buffer.append(cvalue);
						 buffer.append("' union select synonyms from dnms_standard where category='疾病' and standardname like '%");
						 buffer.append(cvalue); 
						 buffer.append("%' ");
					 }
					 
					 buffer.append("))");
				 }
				 
			 }
	       return buffer.toString();
		 
	 }
	 
	 private String sql4bingfabing(String cname ,HttpServletRequest request){
		 StringBuffer buffer = new StringBuffer(" (select disid from DNMS_PARADIS where ");
			//值
			 String c_value = cname+"_value";
			 //逻辑关系，并且 或者
			 String c_logical = cname+"_logical";
			 //字段的名字
			 String c_sel = cname+"_sel";
			 //精确或模糊
			 String c_special = cname+"_special";
			//正名或异名查询
			 String c_syn = cname+"_syn";
			 
			 String cvalue = request.getParameter(c_value);
			 String clogical = request.getParameter(c_logical);
			 String csel = request.getParameter(c_sel);
			 String cspecial = request.getParameter(c_special);
			 String csyn = request.getParameter(c_syn);

			 if(StringUtils.isEmpty(cvalue) ){
				 return null;
			 }
			 if(StringUtils.isNotEmpty(cvalue)){
				 if("0".equals(csyn)){
					 if("0".equals(cspecial)){
						 buffer.append(" PARADISNAME ='");
						 buffer.append(cvalue);
						 buffer.append("' ");
					 }
					 if("1".equals(cspecial)){
						 buffer.append(" PARADISNAME like '%");
						 buffer.append(cvalue);
						 buffer.append("%' ");
					 }
					 buffer.append(")");
				 }else if("1".equals(csyn)){
					 if("0".equals(cspecial)){
						 buffer.append(" PARADISNAME in(select synonyms from dnms_standard where category='并发病' and standardname ='");
						 buffer.append(cvalue);
						 buffer.append("' ");
					 }
					 if("1".equals(cspecial)){
						 buffer.append(" PARADISNAME in(select synonyms from dnms_standard where category='并发病' and standardname like '%");
						 buffer.append(cvalue);
						 buffer.append("%' ");
					 }
					 buffer.append("))");
				 }else if("2".equals(csyn)){
					 if("0".equals(cspecial)){
						 buffer.append(" PARADISNAME in(select standardname from dnms_standard where category='疾病' and synonyms ='");
						 buffer.append(cvalue);
						 buffer.append("' union select synonyms from dnms_standard where category='疾病' and standardname ='");
						 buffer.append(cvalue);
						 buffer.append("' ");
					 }
					 if("1".equals(cspecial)){
						 buffer.append(" PARADISNAME in(select standardname from dnms_standard where category='疾病' and synonyms like '%");
						 buffer.append(cvalue);
						 buffer.append("' union select synonyms from dnms_standard where category='疾病' and standardname like '%");
						 buffer.append(cvalue); 
						 buffer.append("%' ");
					 }
					 
					 buffer.append("))");
				 }
				 
			 }
	       return buffer.toString();
		 
	 }
	 /**
	  * 证候查询
	  * @param request
	  * @return
	  */
	 private String sql4zhenghou(String cname ,HttpServletRequest request){
 		 StringBuffer buffer = new StringBuffer("(select disid from DNMS_SYM where  ");
 		//值
 		 String c_value = cname+"_value";
 		 //逻辑关系，并且 或者
 		 String c_logical = cname+"_logical";
 		 //字段的名字
 		 String c_sel = cname+"_sel";
 		 //精确或模糊
 		 String c_special = cname+"_special";
 		//正名或异名查询
		 String c_syn = cname+"_syn";
		 
		 String cvalue = request.getParameter(c_value);
 		 String clogical = request.getParameter(c_logical);
 		 String csel = request.getParameter(c_sel);
 		 String cspecial = request.getParameter(c_special);
		 String csyn = request.getParameter(c_syn);
		 if(StringUtils.isEmpty(cvalue)){
			 return null;
		 }
		 if(StringUtils.isNotEmpty(cvalue)){
			 
			 if("0".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" SYMPTOMNAME ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" SYMPTOMNAME like '%");
					 buffer.append(cvalue);
					 buffer.append("%' ");
				 }
				 buffer.append(")");
			 }else if("1".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" SYMPTOMNAME in(select synonyms from dnms_standard where category='证候' and standardname ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" SYMPTOMNAME in(select synonyms from dnms_standard where category='证候' and standardname like '%");
					 buffer.append(cvalue);
					 buffer.append("%' ");
				 } 
				 buffer.append("))");
			 }else if("2".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" SYMPTOMNAME in(select standardname from dnms_standard where category='疾病' and synonyms ='");
					 buffer.append(cvalue);
					 buffer.append("' union select synonyms from dnms_standard where category='疾病' and standardname ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" SYMPTOMNAME in(select standardname from dnms_standard where category='疾病' and synonyms like '%");
					 buffer.append(cvalue);
					 buffer.append("' union select synonyms from dnms_standard where category='疾病' and standardname like '%");
					 buffer.append(cvalue); 
					 buffer.append("%' ");
				 }
				 
				 buffer.append("))");
			 }
		 }
		 return buffer.toString();
	 }
	 
	 /**
	  * 方剂查询
	  * @param request
	  * @return
	  */
	 private String sql4fangji(String cname ,HttpServletRequest request){
		 StringBuffer buffer = new StringBuffer("(select disid from DNMS_SYM where id in(select SYMID from DNMS_PRE p where ");
		//值
 		 String c_value = cname+"_value";
 		 //逻辑关系，并且 或者
 		 String c_logical = cname+"_logical";
 		 //字段的名字
 		 String c_sel = cname+"_sel";
 		 //精确或模糊
 		 String c_special = cname+"_special";
 		//正名或异名查询
		 String c_syn = cname+"_syn";
		 
 		 String cvalue = request.getParameter(c_value);
 		 String clogical = request.getParameter(c_logical);
 		 String csel = request.getParameter(c_sel);
 		 String cspecial = request.getParameter(c_special);
		 String csyn = request.getParameter(c_syn);;
		 
		 if(StringUtils.isEmpty(cvalue)){
			 return null;
		 }
		 
		 if(StringUtils.isNotEmpty(cvalue)){
			 if("0".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" PRENAME ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" PRENAME like '%");
					 buffer.append(cvalue);
					 buffer.append("%' ");
				 }
				 buffer.append("))");
			 }else if("1".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" PRENAME in(select synonyms from dnms_standard where category='方剂' and standardname ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" PRENAME in(select synonyms from dnms_standard where category='方剂' and standardname like '%");
					 buffer.append(cvalue);
					 buffer.append("%' ");
				 } 
				 buffer.append(")))");
			 }else if("2".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" PRENAME in(select standardname from dnms_standard where category='疾病' and synonyms ='");
					 buffer.append(cvalue);
					 buffer.append("' union select synonyms from dnms_standard where category='疾病' and standardname ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" PRENAME in(select standardname from dnms_standard where category='疾病' and synonyms like '%");
					 buffer.append(cvalue);
					 buffer.append("' union select synonyms from dnms_standard where category='疾病' and standardname like '%");
					 buffer.append(cvalue); 
					 buffer.append("%' ");
				 }
				 
				 buffer.append(")))");
			 }
		 }
		 return buffer.toString();
	 }
	 /**
	  * 中药查询
	  * @param request
	  * @return
	  */
	 private String sql4zhongyao(String cname ,HttpServletRequest request){
		 StringBuffer buffer = new StringBuffer("(select disid from DNMS_SYM where id in(select symid from DNMS_PRE where id in( select preid from DNMS_HERB h where ");
		//值
 		 String c_value = cname+"_value";
 		 //逻辑关系，并且 或者
 		 String c_logical = cname+"_logical";
 		 //字段的名字
 		 String c_sel = cname+"_sel";
 		 //精确或模糊
 		 String c_special = cname+"_special";
 		//正名或异名查询
		 String c_syn = cname+"_syn";
		 
		 String cvalue = request.getParameter(c_value);
 		 String clogical = request.getParameter(c_logical);
 		 String csel = request.getParameter(c_sel);
 		 String cspecial = request.getParameter(c_special);
		 String csyn = request.getParameter(c_syn);
 		 if(StringUtils.isEmpty(cvalue)){
			 return null;
		 }
		 if(StringUtils.isNotEmpty(cvalue)){
			 if("0".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" HERBNAME ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" HERBNAME like '%");
					 buffer.append(cvalue);
					 buffer.append("%' ");
				 }
				 buffer.append(")))");
			 }else if("1".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" HERBNAME in(select synonyms from dnms_standard where category='中药' and standardname ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" HERBNAME in(select synonyms from dnms_standard where category='中药' and standardname like '%");
					 buffer.append(cvalue);
					 buffer.append("%' ");
				 } 
				 buffer.append("))))");
			 }else if("2".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" HERBNAME in(select standardname from dnms_standard where category='疾病' and synonyms ='");
					 buffer.append(cvalue);
					 buffer.append("' union select synonyms from dnms_standard where category='疾病' and standardname ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" HERBNAME in(select standardname from dnms_standard where category='疾病' and synonyms like '%");
					 buffer.append(cvalue);
					 buffer.append("' union select synonyms from dnms_standard where category='疾病' and standardname like '%");
					 buffer.append(cvalue); 
					 buffer.append("%' ");
				 }
				 
				 buffer.append("))))");
			 }
		 }
		 return buffer.toString();
	 }
	 /**
	  * 穴位组查询
	  * @param request
	  * @return
	  */
	 private String sql4xueweizu(String cname ,HttpServletRequest request){
		 StringBuffer buffer = new StringBuffer("(select disid from DNMS_SYM where id in(select symid from DNMS_POINTGROUP where ");
		//值
 		 String c_value = cname+"_value";
 		 //逻辑关系，并且 或者
 		 String c_logical = cname+"_logical";
 		 //字段的名字
 		 String c_sel = cname+"_sel";
 		 //精确或模糊
 		 String c_special = cname+"_special";
 		//正名或异名查询
		 String c_syn = cname+"_syn";
		 
		 String cvalue = request.getParameter(c_value);
 		 String clogical = request.getParameter(c_logical);
 		 String csel = request.getParameter(c_sel);
 		 String cspecial = request.getParameter(c_special);
		 String csyn = request.getParameter(c_syn);
 		if(StringUtils.isEmpty(cvalue)){
			 return null;
		 }
		 if(StringUtils.isNotEmpty(cvalue)){
			 if("0".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" PGROUPNAME ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" PGROUPNAME like '%");
					 buffer.append(cvalue);
					 buffer.append("%' ");
				 }
				 buffer.append("))");
			 }else if("1".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" PGROUPNAME in(select synonyms from dnms_standard where category='针灸组' and standardname ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" PGROUPNAME in(select synonyms from dnms_standard where category='针灸组' and standardname like '%");
					 buffer.append(cvalue);
					 buffer.append("%' ");
				 } 
				 buffer.append(")))");
			 }else if("2".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" PGROUPNAME in(select standardname from dnms_standard where category='疾病' and synonyms ='");
					 buffer.append(cvalue);
					 buffer.append("' union select synonyms from dnms_standard where category='疾病' and standardname ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" PGROUPNAME in(select standardname from dnms_standard where category='疾病' and synonyms like '%");
					 buffer.append(cvalue);
					 buffer.append("' union select synonyms from dnms_standard where category='疾病' and standardname like '%");
					 buffer.append(cvalue); 
					 buffer.append("%' ");
				 }
				 
				 buffer.append(")))");
			 }
		 }
		 return buffer.toString();
		 
	 }
	 /**
	  * 穴位查询
	  * @param request
	  * @return
	  */
	 private String sql4xuewei(String cname ,HttpServletRequest request){
		 StringBuffer buffer = new StringBuffer("(select disid from DNMS_SYM where id in(select symid from DNMS_POINTGROUP where id in( select PGROUPID from DNMS_POINT t where  ");
			//值
	 		 String c_value = cname+"_value";
	 		 //逻辑关系，并且 或者
	 		 String c_logical = cname+"_logical";
	 		 //字段的名字
	 		 String c_sel = cname+"_sel";
	 		 //精确或模糊
	 		 String c_special = cname+"_special";
	 		//正名或异名查询
			 String c_syn = cname+"_syn";
			 
			 String cvalue = request.getParameter(c_value);
	 		 String clogical = request.getParameter(c_logical);
	 		 String csel = request.getParameter(c_sel);
	 		 String cspecial = request.getParameter(c_special);
			 String csyn = request.getParameter(c_syn);
	 		if(StringUtils.isEmpty(cvalue)){
				 return null;
			 }
			 if(StringUtils.isNotEmpty(cvalue)){
				 if("0".equals(csyn)){
					 if("0".equals(cspecial)){
						 buffer.append(" POINTNAME ='");
						 buffer.append(cvalue);
						 buffer.append("' ");
					 }
					 if("1".equals(cspecial)){
						 buffer.append(" POINTNAME like '%");
						 buffer.append(cvalue);
						 buffer.append("%' ");
					 }
					 buffer.append(")))");
				 }else if("1".equals(csyn)){
					 if("0".equals(cspecial)){
						 buffer.append(" POINTNAME in(select synonyms from dnms_standard where category='针灸组' and standardname ='");
						 buffer.append(cvalue);
						 buffer.append("' ");
					 }
					 if("1".equals(cspecial)){
						 buffer.append(" POINTNAME in(select synonyms from dnms_standard where category='针灸组' and standardname like '%");
						 buffer.append(cvalue);
						 buffer.append("%' ");
					 } 
					 buffer.append("))))");
				 }else if("2".equals(csyn)){
					 if("0".equals(cspecial)){
						 buffer.append(" POINTNAME in(select standardname from dnms_standard where category='疾病' and synonyms ='");
						 buffer.append(cvalue);
						 buffer.append("' union select synonyms from dnms_standard where category='疾病' and standardname ='");
						 buffer.append(cvalue);
						 buffer.append("' ");
					 }
					 if("1".equals(cspecial)){
						 buffer.append(" POINTNAME in(select standardname from dnms_standard where category='疾病' and synonyms like '%");
						 buffer.append(cvalue);
						 buffer.append("' union select synonyms from dnms_standard where category='疾病' and standardname like '%");
						 buffer.append(cvalue); 
						 buffer.append("%' ");
					 }
					 
					 buffer.append("))))");
				 }
			 }
			 return buffer.toString();
	 }
	
	 /**
	  *原文下载
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/search/filelist.do")   
     public ModelAndView filelist(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
         String id = request.getParameter("id");
         DnmsMagazine dm = (DnmsMagazine)dataService.findByID(DnmsMagazine.class, new Long(id));
         modelMap.put("obj",dm);
 		 return new ModelAndView("app/search/filelist",modelMap);
		 
	 } 
	 public String statinfo(HttpServletRequest request,String allsql){
		 StringBuffer buffer = new StringBuffer("共检索到");
		 
		 //检索出多少篇文章，涉及证候多少个，方剂多少种，中药多少种，穴位多少种。
		 DBMgr dbo = this.getDbo(request);
		 List symlist = dbo.queryList("select count(*) as countall from (select count(*) ,SYMPTOMNAME from DNMS_SYM where disid  in(select id from DNMS_DISEASE where INDEXID in(select id from ("+allsql+"))) group by SYMPTOMNAME)", null);
		 List pointlist = dbo.queryList("select count(*) as countall from (select count(*) , POINTNAME from DNMS_POINT where PGROUPID in(select id from DNMS_POINTGROUP where symid in(select id from DNMS_SYM where disid in(select id from DNMS_DISEASE where INDEXID in(select id from ("+allsql+"))))) group by POINTNAME)",null); 
		 List prelist = dbo.queryList("select count(*) as countall from (select count(*) ,PRENAME from DNMS_PRE where symid in(select id from DNMS_SYM where disid in(select id from DNMS_DISEASE where INDEXID in(select id from ("+allsql+")))) group by PRENAME)",null);
		 List herblist = dbo.queryList("select count(*) as countall from (SELECT count(*) ,HERBNAME FROM DNMS_HERB WHERE PREID IN(SELECT ID from DNMS_PRE where symid in(select id from DNMS_SYM where disid in(select id from DNMS_DISEASE where INDEXID in(select id from ("+allsql+"))))) group by HERBNAME)",null);
		 List magalist = dbo.queryList("select to_char(count(*)) as paprenum from ("+allsql+")", null);
		 Map symmap = (Map)symlist.get(0);
		 Map pointmap = (Map)pointlist.get(0);
		 Map premap = (Map)prelist.get(0);
		 Map herbmap = (Map)herblist.get(0);
 		 Map magamap = (Map)magalist.get(0); 
		 buffer.append(magamap.get("paprenum")).append("篇文献，");
		 buffer.append("涉及的证候共").append(symmap.get("countall")+"个,");
		 buffer.append("方剂").append(premap.get("countall")+"种,");
		 buffer.append("中药").append(herbmap.get("countall")+"种,");
		 buffer.append("穴位").append(pointmap.get("countall")+"种！");

		 return buffer.toString();
	 }
	 
	 /**
	  * 导出检索结果
	  * @param request
	  * @param response
	  * @return
	 * @throws Exception 
	  */
	@RequestMapping("/search/exportMagaResult.do")   
	public ModelAndView exportMagaResult(HttpServletRequest request,
			HttpServletResponse response) throws Exception { 
		try  {
			String resulttype = request.getParameter("resulttype");
			String magaid = request.getParameter("magaid");
			String allsql = request.getParameter("allsql");
			String path = this.exportData(magaid,allsql,resulttype);
			System.out.println("message::" + path );
			//  path是指欲下载的文件的路径。 
			response.setCharacterEncoding("UTF-8");
			File file  =   new  File(path);
			String filename  =  file.getName();
			int fileLength = (int)file.length();
			int                 length   = 0;
	        ServletOutputStream op       = response.getOutputStream();
	        ServletContext      context  = request.getSession().getServletContext();
	        String              mimetype = context.getMimeType( filename );
	        byte[]   byteContents   =   new   byte[fileLength];   
	        FileInputStream fis   =   new   FileInputStream(file);   
	        int   retcd   =   fis.read(byteContents);   
	        fis.close();    
	        logger.debug("mimetype:" + mimetype);
	        response.setContentType(mimetype == null?"application/vnd.ms-excel":mimetype );
	        response.setContentLength( retcd );
	        response.setHeader( "Content-Disposition", "attachment; filename=\"" + new String(filename.getBytes("gb2312"),"ISO-8859-1") + "\"" );
	        op.write(byteContents,0,retcd); 
	        op.close();
    }  catch  (IOException ex) {
        ex.printStackTrace();
    }
		return null;  
	} 
	/**
	 * 生成题录数据文件
	 * @return 生成的文件的路径
	 */
	public String exportData(String ridstr,String allsql,String resulttype){
		String message = "false";
		//resulttype为1代表选择导出，为2代表导出全部
		if("1".equals(resulttype)){
			if(ridstr.endsWith(",")){
				ridstr = ridstr.substring(0,ridstr.length()-1);
			}
			allsql = "select * from (" + allsql + ") where id in(" + ridstr + ")"; 
		}
		 try {
				HSSFWorkbook wb = new HSSFWorkbook(); 
				 HSSFSheet sheet = wb.createSheet("new sheet"); 
				 HSSFRow row = sheet.createRow((short)0); 
				 row.createCell(0).setCellValue("biaoti_qq"); 
				 row.createCell(1).setCellValue("题名"); 
				 row.createCell(2).setCellValue("英文标题"); 
				 row.createCell(3).setCellValue("刊名"); 
				 row.createCell(4).setCellValue("作者"); 
				 row.createCell(5).setCellValue("英文作者"); 
				 row.createCell(6).setCellValue("第一作者单位"); 
				 row.createCell(7).setCellValue("第一作者地址"); 
				 row.createCell(8).setCellValue("中文关键词"); 
				 row.createCell(9).setCellValue("中文主题词"); 
				 row.createCell(10).setCellValue("英文主题词"); 
				 row.createCell(11).setCellValue("中文摘要"); 
				 row.createCell(12).setCellValue("资助情况"); 
				 row.createCell(13).setCellValue("分类号"); 
				 row.createCell(14).setCellValue("年"); 
				 row.createCell(15).setCellValue("卷"); 
				 row.createCell(16).setCellValue("期"); 
				 row.createCell(17).setCellValue("页码"); 
				 row.createCell(18).setCellValue("英文摘要"); 
				 row.createCell(19).setCellValue("语种"); 
				 row.createCell(20).setCellValue("核心期刊"); 
				 row.createCell(21).setCellValue("中医疾病");
				 row.createCell(22).setCellValue("西医疾病");
				 CellStyle cs=wb.createCellStyle(); 
				 List accList = null;
				 accList = DBMgr.getDbUtilInst().queryList(allsql,null);
				 
				 for(int i=0; i<accList.size(); i++){
						Map data = (Map)accList.get(i);
						HSSFRow onerow = sheet.createRow((short)(i+1)); 
						onerow.createCell(0).setCellValue(String.valueOf(data.get("id"))); 
						onerow.createCell(1).setCellValue((String)data.get("maganame")); 
						onerow.createCell(2).setCellValue((String)data.get("titleeng")); 
						onerow.createCell(3).setCellValue((String)data.get("periodicalname")); 
						onerow.createCell(4).setCellValue((String)data.get("author")); 
						onerow.createCell(5).setCellValue((String)data.get("authoreng")); 
						onerow.createCell(6).setCellValue((String)data.get("authorunit")); 
						onerow.createCell(7).setCellValue((String)data.get("authoradress")); 
						onerow.createCell(8).setCellValue((String)data.get("keywords")); 
						onerow.createCell(9).setCellValue((String)data.get("themewords")); 
						onerow.createCell(10).setCellValue((String)data.get("themewordseng")); 
						onerow.createCell(11).setCellValue((String)data.get("abstract_")); 
						onerow.createCell(12).setCellValue((String)data.get("fund")); 
						onerow.createCell(13).setCellValue((String)data.get("classno")); 
						onerow.createCell(14).setCellValue((String)data.get("year")); 
						onerow.createCell(15).setCellValue((String)data.get("volume")); 
						onerow.createCell(16).setCellValue((String)data.get("period")); 
						onerow.createCell(17).setCellValue((String)data.get("pageno")); 
						onerow.createCell(18).setCellValue((String)data.get("abstracteng")); 
						onerow.createCell(19).setCellValue((String)data.get("language"));
						onerow.createCell(20).setCellValue((String)data.get("sourcemagazine"));
						onerow.createCell(21).setCellValue((String)data.get("diseasename"));
						onerow.createCell(22).setCellValue((String)data.get("diseasenamen"));
				 }	
				 String tempFilepath = SystemUtil.getWebappRoot().toString();
				 tempFilepath = tempFilepath.replace("\\", "/") + "/temp";
				 System.out.println("tempFilepath::" + tempFilepath );
				 File tempfile = new File(tempFilepath);
				 if(!tempfile.exists()){
					 tempfile.mkdirs();
				 }
				 String destfile = tempFilepath + "/" + "多囊卵巢题录数据.xls";
				 FileOutputStream fileOut = new FileOutputStream(destfile); 
				 wb.write(fileOut); 
				 fileOut.close();
				 message = destfile;
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		return message;
		
	}
}
